package com.jz.community.moduleshopping.groupbuying.task;

import android.app.Activity;

import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.task.RxTask;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.view.widget.ProgressDialogManager;
import com.jz.community.moduleshopping.groupbuying.bean.GroupBuyingBean;

import java.util.HashMap;

/**
 * Created by Tony.Fan on 2018/6/06 14:16
 */
public class GroupBuyingTask extends RxTask<String, Integer, GroupBuyingBean> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public GroupBuyingTask(Activity activity,
                           ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected GroupBuyingBean doInBackground(String... params) {
        // 执行请求
        String result = OkHttpUtil.get(Constant.DOMINOS_NEARBY_URL, getParam(params[0], params[1], params[2], params[3]));
        GroupBuyingBean info = null;
        if (!Preconditions.isNullOrEmpty(result)) {
            info = JsonUtils.parseObject(result, GroupBuyingBean.class);
        }
        return info;
    }

    @Override
    protected void onPostExecute(GroupBuyingBean result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    /**
     * 获取请求所需的参数
     *
     * @param
     * @return
     */
    private HashMap<String, Object> getParam(String page, String lat, String lon, String cityZone) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("size", 10);
        params.put("lat", lat);
        params.put("lon", lon);
        params.put("cityZone", cityZone);
        return params;
    }
}